package com.douwong.student.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.base.BaseFragment;
import com.douwong.student.interfaces.QuestionAnswerInteractInterface;
import com.douwong.student.model.QaTopic;
import com.douwong.student.popuwindow.ImageViewPopuwidow;
import com.douwong.student.popuwindow.ShowQADetailPopuWindow;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.UIUtils;
import com.douwong.student.view.MTextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @autor yichao
 * @time 2016/10/2714:08
 * @des 问答互动判断
 */
@SuppressLint("ValidFragment")
public class QuestionAnswerJudgeFragment extends BaseFragment implements QuestionAnswerInteractInterface {
    private Context mContext;
    private QaTopic mQaTopic;
    private TextView mTv_judgment_right;
    private TextView mTv_judgment_wrong;
    private String mJudgMentLastAnswer="";
    private ImageView mImageViewTitle;
    private File mFile;
    private MTextView mTv_judgment_questions;
    private LinearLayout mLinearLayoutRoot;
    private ShowQADetailPopuWindow mQADetailPopuWindow;
    private ImageViewPopuwidow mImageViewPopuwidow;

    public QuestionAnswerJudgeFragment(Context context, QaTopic qaTopic) {
        super();
        this.mContext=context;
        mQaTopic=qaTopic;
    }

    @Override
    protected void initEvent() {
        mTv_judgment_right.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                v.setSelected(true);
                if (v.isSelected()) {

                    mJudgMentLastAnswer = "Yes";
                }
                mTv_judgment_wrong.setSelected(false);
            }
        });

        mTv_judgment_wrong.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mTv_judgment_right.setSelected(false);
                v.setSelected(true);
                if (v.isSelected()) {
                    mJudgMentLastAnswer = "No";
                }
            }
        });
        mLinearLayoutRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mQADetailPopuWindow = new ShowQADetailPopuWindow(mContext,mQaTopic.getSubjectType(),mQaTopic.getBody(),mQaTopic.getOptions(),mFile==null?null:mFile.getAbsolutePath());
             mQADetailPopuWindow.showAtLocation(v,Gravity.CENTER,0,0);

            }
        });
    }
    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        View   mJudgeView = UIUtils.inflate(R.layout.judgment_questions);
        mTv_judgment_questions = (MTextView) mJudgeView.findViewById(R.id.tv_question_choice);
        mTv_judgment_right = (TextView) mJudgeView.findViewById(R.id.tv_Judgment_right_question);
        mTv_judgment_wrong = (TextView) mJudgeView.findViewById(R.id.tv_Judgment_wrong_question);
        mImageViewTitle = (ImageView) mJudgeView.findViewById(R.id.iv_answer_show_pic);
        mLinearLayoutRoot = (LinearLayout) mJudgeView.findViewById(R.id.ll_choicefragme_title_root);
        return mJudgeView;
    }

    @Override
    protected void initData() {
        super.initData();
        boolean showIcon = isShowIcon(mQaTopic);
        if (showIcon){
            mImageViewTitle.setVisibility(View.VISIBLE);
        }
        //设置题目
        ImagFilterUtil.filterMTImage(mTv_judgment_questions,mQaTopic.getBody());
        mImageViewTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFile!=null){
                    mImageViewPopuwidow = new ImageViewPopuwidow(mContext,mFile);
                    mImageViewPopuwidow.showAtLocation(getView(), Gravity.CENTER, 0, 0);
                }
            }
        });
    }

    /**
     * 判断是否显示标题图片
     * @param mQditem
     */
    private boolean isShowIcon(QaTopic mQditem) {
        if (mQditem!=null&&mQditem.getImageUrl()!=null&&mQditem.getImageUrl().length()!=0){
            return true;
        }
        return false;
    }

    @Override
    public List<String> getAnswerInfo() {
        List<String> list=new ArrayList<>();
        list.add(mJudgMentLastAnswer);
        return list;
    }

    @Override
    public void setCommitStatue() {
        mTv_judgment_right.setClickable(false);
        mTv_judgment_wrong.setClickable(false);

    }

    @Override
    public void setImageView(File file, Bitmap bitmap) {
        mFile=file;
        mImageViewTitle.setImageBitmap(bitmap);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mImageViewPopuwidow!=null &&mImageViewPopuwidow.isShowing()){
            mImageViewPopuwidow.dismiss();
        }
    }
}
